package sliver.action.shop.ajax;

import java.util.HashMap;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class TaoBaoFav {
	/**
	 * 提供淘宝的账号、密码和宝贝地址就能添加到收藏夹
	 * 
	 * @param username
	 *            淘宝登录账号
	 * @param password
	 *            淘宝密码
	 * @param itemUrl
	 *            宝贝地址
	 * @return
	 */
	public static boolean taobao(String username, String password,
			String itemUrl) {
		try {
			Map<String, String> paras = new HashMap<String, String>();
			Map<String, String> cookiesTemp = null; // 每个页面的cookie

			Map<String, String> cookies = new HashMap<String, String>(); // 所有页面的cookie

			// 登录页面
			Connection conn = Jsoup.connect("https://login.taobao.com/");
			conn.followRedirects(true);
			Response response = conn.execute();
			// 保存cookie
			cookiesTemp = response.cookies();
			for (String key : cookiesTemp.keySet()) {
				cookies.put(key, cookiesTemp.get(key));
			}

			// 获取所有的input的key和value
			Document doc = response.parse();
			Elements els = doc.select("form#J_StaticForm input");
			String key;
			String val;
			for (Element el : els) {
				key = el.attr("name");
				val = el.attr("value");
				if (key.isEmpty()) {
					continue;
				}
				paras.put(key, val);
			}

			// 设置登录密码
			paras.put("TPL_username", username);
			paras.put("TPL_password", password);

			// 登录
			Connection conn2 = Jsoup
					.connect("https://login.taobao.com/member/login.jhtml");
			conn2.method(Method.POST);
			conn2.cookies(cookies);
			conn2.data(paras);
			conn2.followRedirects(true);
			Response response2 = conn2.execute();

			cookiesTemp = response2.cookies();
			for (String key1 : cookiesTemp.keySet()) {
				cookies.put(key1, cookiesTemp.get(key1));
			}

			// 获取宝贝页面，抓去收藏按钮的href
			Connection conn3 = Jsoup.connect(itemUrl);
			conn3.cookies(cookies);
			Response response3 = conn3.execute();
			cookiesTemp = response3.cookies();

			for (String key1 : cookiesTemp.keySet()) {
				cookies.put(key1, cookiesTemp.get(key1));
			}
			Document itemDoc = response3.parse();
			String favLink = itemDoc
					.select("li.tb-trash-fav a.J_TDialogTrigger").first()
					.attr("href");

			// 获取收藏页面
			Connection conn4 = Jsoup.connect(favLink);
			conn4.cookies(cookies);
			Response response4 = conn4.execute();
			cookiesTemp = response4.cookies();

			for (String key1 : cookiesTemp.keySet()) {
				cookies.put(key1, cookiesTemp.get(key1));
			}
			Document favDoc = response4.parse();
			Map<String, String> favMap = new HashMap<String, String>();
			els = favDoc.select("form#PopupFavorForm input");
			for (Element el : els) {
				key = el.attr("name");
				val = el.attr("value");
				if (key.isEmpty()) {
					continue;
				}
				favMap.put(key, val);
			}

			// 提交收藏
			Connection conn5 = Jsoup
					.connect("http://favorite.taobao.com/popup/add_collection.htm");
			conn5.cookies(cookies);
			conn5.method(Method.POST);
			conn5.data(favMap);
			conn5.execute();

		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

	public static boolean tmall(String username, String password, String itemUrl) {
		try {
			Map<String, String> paras = new HashMap<String, String>();
			Map<String, String> cookiesTemp = null; // 每个页面的cookie

			Map<String, String> cookies = new HashMap<String, String>(); // 所有页面的cookie

			// 到首页获取登录页面地址
			Connection conn = Jsoup.connect(itemUrl);
			Response response = conn.execute();
			Document doc = conn.get();
			cookiesTemp = response.cookies();
			for (String key1 : cookiesTemp.keySet()) {
				cookies.put(key1, cookiesTemp.get(key1));
			}
			String favUrl = doc.select("a#J_Favorite").first().attr("href");

			Connection conn1 = Jsoup.connect(favUrl);
			conn1.method(Method.GET);
			conn1.followRedirects(false);
			conn1.cookies(cookies);
			Response response1 = conn1.execute();
			for (String key1 : cookiesTemp.keySet()) {
				cookies.put(key1, cookiesTemp.get(key1));
			}
			String location = response1.header("Location");

			// 登录页面
			Connection conn2 = Jsoup.connect(location);
			conn2.followRedirects(true);
			Response response2 = conn2.execute();
			// 保存cookie
			cookiesTemp = response2.cookies();
			for (String key : cookiesTemp.keySet()) {
				cookies.put(key, cookiesTemp.get(key));
			}

			// 获取所有的input的key和value
			Document doc2 = response2.parse();
			Elements els = doc2.select("form#J_StaticForm input");
			String key;
			String val;
			for (Element el : els) {
				key = el.attr("name");
				val = el.attr("value");
				if (key.isEmpty()) {
					continue;
				}
				paras.put(key, val);
			}
			// 设置登录密码
			paras.put("TPL_username", username);
			paras.put("TPL_password", password);
			
			// 登录
			Connection conn3 = Jsoup
					.connect("https://login.taobao.com/member/login.jhtml");
			conn3.method(Method.POST);
			conn3.cookies(cookies);
			conn3.data(paras);
			conn3.followRedirects(true);
			Response response3 = conn3.execute();
			cookiesTemp = response3.cookies();
			for (String key1 : cookiesTemp.keySet()) {
				cookies.put(key1, cookiesTemp.get(key1));
			}
			Document favDoc = response3.parse();
			Map<String, String> favMap = new HashMap<String, String>();
			els = favDoc.select("form#PopupFavorForm input");
			for (Element el : els) {
				key = el.attr("name");
				val = el.attr("value");
				if (key.isEmpty()) {
					continue;
				}
				favMap.put(key, val);
			}

			// 提交收藏
			Connection conn5 = Jsoup
					.connect("http://favorite.taobao.com/popup/add_collection.htm");
			conn5.cookies(cookies);
			conn5.method(Method.POST);
			conn5.data(favMap);
			conn5.execute();

		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

	public static boolean add(String username, String password, String itemUrl) {
		if (itemUrl.startsWith("http://detail.tmall.com")) {
			return tmall(username, password, itemUrl);
		} else {
			return taobao(username, password, itemUrl);
		}
	}
}
